package me.w70.bot.scripting;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.HashMap;
import java.util.Vector;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class GUIBuilder implements ActionListener, ItemListener {

	JFrame base;

	JPanel area;

	HashMap args = new HashMap();
	HashMap isChecked = new HashMap();
	Script s;



	public GUIBuilder(Script s) {
		this.s = s;
		base = new JFrame();
		area = new JPanel();
		// area.setLayout(new BoxLayout(area, BoxLayout.Y_AXIS));
		base.getContentPane().add(area, BorderLayout.CENTER);
		base.setTitle("Settings for: " + s.getScriptName());
		base.setPreferredSize(new Dimension(300, 300));

		
	}

	public void display() {
		JButton submit = new JButton("Start");
		submit.addActionListener(this);
		area.add(submit);
		int height = 0;
		for (Component c : area.getComponents()) {
			height += c.getPreferredSize().height;
		}
		base.setPreferredSize(new Dimension(400, height + 20));
		base.setLocation(new Point(0,0));
		base.pack();
		base.setVisible(true);
		base.toFront();
		base.requestFocus();
	}

	public void placeArgs() {
		for (Component c : area.getComponents()) {
			if (c instanceof JPanel) {
				JPanel o = (JPanel) c;
				for (Component d : o.getComponents()) {
					if (d.getName() != null) {
						if(d instanceof JCheckBox) {
							JCheckBox ch = (JCheckBox)d;
							args.put(d.getName(), ch.isSelected());
						}
						if(d instanceof JTextField) {
							JTextField t = (JTextField)d;
							args.put(d.getName(), t.getText());
						}
						if(d instanceof JComboBox) {
							JComboBox cb = (JComboBox)d;
							args.put(d.getName(), cb.getItemAt(cb.getSelectedIndex()));
						}
					}
				}
			}
		}
		s.doneGUI = true;
	}
	
	public HashMap getArgs() {
		return args;
	}

	public void addCheckBox(String label, String option, boolean checked) {
		JPanel pane = new JPanel();
		JCheckBox box = new JCheckBox(label, checked);
		box.addActionListener(this);
		box.setName(option);
		box.addItemListener(this);
		
		pane.add(box);
		area.add(pane);
		args.put(option, checked);
	}

	public void addTextbox(String label, String option) {
		JPanel pane = new JPanel();

		JTextField field = new JTextField();
		field.setName(option);
		field.setPreferredSize(new Dimension(200, 20));
		pane.add(field);
		area.add(pane);
		args.put(option, "");
	}

	public void addSelection(String label, String option, Vector<Object> options) {
		JPanel pane = new JPanel();
		DefaultComboBoxModel optionsModel = new DefaultComboBoxModel();
		for (int i = 0; i < options.size(); i++) {
			optionsModel.addElement(options.get(i));
		}
		JComboBox selection = new JComboBox(optionsModel);
		selection.setName(option);
		JLabel gLabel = new JLabel(label);
		pane.add(gLabel);
		pane.add(selection);
		area.add(pane);
		args.put(option, options.get(0));
	}

	public void actionPerformed(ActionEvent a) {
		
		if (a.getActionCommand().equals("Start")) {
			placeArgs();
			base.setVisible(false);
			s.afterGUI(s.gui.getArgs());
			s.running = true;
			if (!s.hasRun()) {
				s.start();
			}
		}
	}

	public void itemStateChanged(ItemEvent i) {
	
	}

}
